import axios from 'axios'
import config from '../config/config'
import packageInfo from "../../package";

const ajax = (url, data={}, token = '', type='GET') => {
    url = config.SERVER.HOST + url
    return new Promise((resolve, reject) => {
        let promise
        let headers = {
            'Content-Type': 'application/json',
            'X-User-System-ID': 'CLIENT_USER',
            'version': packageInfo.version,
            'Authorization':''
        };
        if (token) {
            headers['Authorization'] = 'Bearer ' + token;
        }
        if (type === 'GET') {
            promise = axios({
                url,
                method: type,
                headers: headers,
                params: data
            })
        } else {
            promise = axios({
                url,
                method: type,
                headers: headers,
                data
            })
            // console.log(data, 'data')
        }
        promise
            .then(res => resolve(res.data))
            .catch(err => console.log('出错了-->>>>>', err))
    })
}

// 登录heartbeat
export const login = ({username, password}) => ajax('/client/login', {username, password}, '', 'POST')
// heartbeat
export const heartbeat = (payload, token) => ajax('/heartbeat', {payload}, token, 'POST')
// 获取products列表
export const getProducts = (id, token) => ajax(`/client-products/CLIENT/${id}`, {}, token)
// 获取用户信息
export const getUserInfo = (token) => ajax('/client/getInfo', {}, token)
// product 详情
export const getProductDetail = (id, token) => ajax(`/client-product-users/${id}`, {}, token)
// 重新设置cookie
export const resetSetCookie = ({cookies, productUserId}, token) => ajax('/callback', {cookies, productUserId}, token, 'POST')

//获取内部自定义功能使用次数
export const getProductFunctionUseCount = (productId, type, token) => ajax('/custom-time/getProductFunctionUseCount', {productId, type}, token,'POST')

// 退出登录
export const logout = (token) => ajax(`/logout`, {}, token)

// // 更新分类
// export const reqUpdateCategory = (categoryId, categoryName) => ajax('/manage/category/update', {
//     categoryId,
//     categoryName
// }, 'POST')

